package edu.uwm.cs552;

import javax.swing.JComboBox;


/**
 * A combo-box for choosing a Good.
 * The selected item is always null or a Good.
 */
public class GoodComboBox extends JComboBox {
	/**
	 * Eclipse wants this.
	 */
	private static final long serialVersionUID = 1L;
	
	private static Good[] goodsArray;
	
	private static Good[] getGoodsArray() {
		if (goodsArray == null) {
			Good[] regularGoodArray = Good.allGoods();
			goodsArray = new Good[regularGoodArray.length+1];
			for (int i = 0; i < regularGoodArray.length; ++i) {
				goodsArray[i+1] = regularGoodArray[i];
			}
		}
		return goodsArray;
	}
	
	public GoodComboBox() {
		super(getGoodsArray());
		setRenderer(new GoodRenderer());
	}
	
	@Override
	public Good getSelectedItem() {
		return (Good)super.getSelectedItem();
	}
	
	@Override
	public void setSelectedItem(Object x) {
		super.setSelectedItem((Good)x); // may throw exception
	}
}
